import {extend} from "../../../support/extend";
import {Field} from "../field/field.model";
import {generateFieldId} from "../../../support/generateFieldId";

class Rect {
  width: number = 0;
  height: number = 0;
  scrollWidth: number = 0;
  scrollHeight: number = 0;
}

enum CellType {
  sequence = "cellSequence",
  batch = "cellBatch",
  add = "cellAdd",
  data = "data",
}

class Cell {
  type: CellType = CellType.data;
  field: Field = new Field();
  left?: number = 0;
  width?: number = 240;
  isNew?: boolean = false;

  constructor(type?: string) {
    this.field.type = type;
  }

  static generateNew(cellType: CellType, field?: Field): Cell {
    let cell = new Cell();
    cell.type = cellType;
    // 数据列
    if (cellType == CellType.data) {
      // 新增
      if (!field) {
        cell.isNew = true;
        cell.field.type = "text";
        cell.field.fieldId = generateFieldId();
      } else {
        cell.field = field;
      }
    }
    // 其他列
    else {
      cell.field.type = cell.field.fieldId = cellType;
    }
    return cell
  }

  static copy(oldCell: Cell): Cell {
    let newCell = Cell.generateNew(oldCell.type, extend({}, oldCell.field))
    newCell.field.fieldId = generateFieldId();
    newCell.field.name = newCell.field.name + " 副本";
    return newCell;
  }
}

class Item {
  fields: any = {};
  // values: object = {};
}

class Filed {
  fieldId: number;
  values: Array<Value> = new Array<Value>();
}

class Value {
  displayValue: string;
  value: any;
}

class TableModel {
  isDisplaySequence: boolean = true;
  displayAddCell: boolean = false;
  isCompact?: boolean;
  items: Array<Item> = new Array<Item>();
  width: number = 0;
  height: number = 0;
  displayFieldIds: Array<number>;
  fields: any;
  fieldLayout: Array<number>;
  private _cells: Array<Cell>;

  constructor(config: any) {
    extend(this, config);
  }


  set cells(value: Array<Cell>) {
    this._cells = value;
    // console.log(value)
  }

  get cells(): Array<Cell> {
    return this._cells;
  }
}

class Table {
  // rect: Rect = {width: 0, height: 0};
  // 字段
  fields: Array<Field> = new Array<Field>();
  // 列头
  cellIds: Array<string> = new Array<string>();
  cellObjs: any = {};
}

export {Rect, CellType, Cell, Item, Filed, Value, Table, TableModel}
